package com.weifly.weistock.core.market.impl;

import com.weifly.weistock.core.common.StockException;
import com.weifly.weistock.core.market.StockPriceDto;
import com.weifly.weistock.core.market.StockMarketService;
import com.weifly.weistock.core.util.HttpRequestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 股票行情软件
 *
 * @author weifly
 * @since 2019/2/3
 */
public class SinaStockMarketService implements StockMarketService {

    private Logger logger = LoggerFactory.getLogger(SinaStockMarketService.class);

    public static final String STOCK_URL_PREFIX = "http://hq.sinajs.cn/list=";

    /**
     * 获得股票价格信息
     * @param stockCode
     * @return
     */
    public StockPriceDto getStockPrice(String stockCode){
        String fullCode = null;
        String exchangeId = null;
        Double minStepPrice = null;
        if(stockCode.startsWith("6")){
            fullCode = "sh"+stockCode; // 上海股票
            exchangeId = "1";
            minStepPrice = 0.01;
        }else if(stockCode.startsWith("5")){
            fullCode = "sh"+stockCode; // 上海ETF基金
            exchangeId = "1";
            minStepPrice = 0.001;
        }else if(stockCode.startsWith("1")){
            fullCode = "sz"+stockCode; // 深圳ETF基金
            exchangeId = "0";
            minStepPrice = 0.001;
        }else if(stockCode.startsWith("0") || stockCode.startsWith("3")){
            fullCode = "sz"+stockCode; // 深圳股票
            exchangeId = "0";
            minStepPrice = 0.01;
        }else{
            throw new StockException("不支持证券代码：" + stockCode);
        }

        String url = STOCK_URL_PREFIX+fullCode;
        String stockInfoStr =  HttpRequestUtils.sendGet(url, "GBK");
        if(stockInfoStr==null){
            return null;
        }
        int startIdx = stockInfoStr.indexOf("\"");
        if(startIdx==-1){
            return null;
        }
        int endIdx = stockInfoStr.indexOf("\"", startIdx+1);
        if(endIdx==-1){
            return null;
        }
        String targetStr = stockInfoStr.substring(startIdx+1, endIdx);
        //System.out.println(targetStr);
        String[] infoParts = targetStr.split(",");
        if(infoParts.length<32){
            return null;
        }

        StockPriceDto priceInfo = new StockPriceDto();
        priceInfo.setCode(stockCode);
        priceInfo.setExchangeId(exchangeId);
        priceInfo.setMinStepPrice(minStepPrice);
        priceInfo.setName(infoParts[0]); // 0 - 证券名称
        // 1 - 开盘价
        // 2 - 昨天收盘价
        priceInfo.setNowPrice(Double.valueOf(infoParts[3])); // 3 - 最新价
        priceInfo.setHighPrice(Double.valueOf(infoParts[4])); // 4 - 最高价
        priceInfo.setLowPrice(Double.valueOf(infoParts[5])); // 5 - 最低价
        // 6 - 竞买价，即“买一”报价
        // 7 - 竞卖价，即“卖一”报价
        // 8 - 成交的股票数
        // 9 - 成交金额
        // 10 - 买一申请股数
        // 11 - 买一报价
        // 12 - 买二申请股数
        // 13 - 买二报价
        // 14 - 买三申请股数
        // 15 - 买三报价
        // 16 - 买四申请股数
        // 17 - 买四报价
        // 18 - 买五申请股数
        // 19 - 买五报价
        // 20 - 卖一申请股数
        // 21 - 卖一报价
        // 22 - 卖二申请股数
        // 23 - 卖二报价
        // 24 - 卖三申请股数
        // 25 - 卖三报价
        // 26 - 卖四申请股数
        // 27 - 卖四报价
        // 28 - 卖五申请股数
        // 29 - 卖五报价
        priceInfo.setPriceDate(infoParts[30]); // 30 - 日期
        priceInfo.setPriceTime(infoParts[31]); // 31 - 时间
        priceInfo.setUpdateTime(System.currentTimeMillis());
        return priceInfo;
    }
}
